home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / krandomsequence.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-19  |  4.2 KB  |  146 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (c) 1999 Sean Harmer <sh@astro.keele.ac.uk>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License version 2 as published by the Free Software Foundation.
  7.  
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU Library General Public License
  14.    along with this library; see the file COPYING.LIB.  If not, write to
  15.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16.    Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef K_RANDOM_SEQUENCE_H
  19. #define K_RANDOM_SEQUENCE_H
  20.  
  21. #include "kdelibs_export.h"
  22.  
  23. class KRandomSequencePrivate;
  24. class QGList;
  25. /**
  26.  * A class to create a pseudo-random sequence
  27.  *
  28.  * Given a seed number, this class will produce a sequence of
  29.  * pseudo-random numbers. This would typically be used in
  30.  * applications like games.
  31.  *
  32.  * In general, you should instantiate a KRandomSequence object and
  33.  * pass along your seed number in the constructor. From then on,
  34.  * simply call getDouble() or getLong() to obtain the next
  35.  * number in the sequence.
  36.  *
  37.  * @author Sean Harmer <sh@astro.keele.ac.uk>
  38.  */
  39. class KDECORE_EXPORT KRandomSequence
  40. {
  41. public: 
  42.   /**
  43.    * Creates a pseudo-random sequence based on the seed lngSeed.
  44.    *
  45.    * A Pseudo-random sequence is different for each seed but can be 
  46.    * reproduced by starting the sequence with the same seed.
  47.    *
  48.    * If you need a single value which needs to be unpredictable, 
  49.    * you need to use kapp->random() instead.
  50.    * 
  51.    * @param lngSeed Seed to initialize the sequence with.
  52.    * If lngSeed is 0, the sequence is initialized with a value from
  53.    * KApplication::random().
  54.    */
  55.   KRandomSequence( long lngSeed = 0 );
  56.  
  57.   /**
  58.    * Standard destructor
  59.    */
  60.   virtual ~KRandomSequence();
  61.  
  62.   /**
  63.    * Copy constructor
  64.    */
  65.   KRandomSequence(const KRandomSequence &a);
  66.   
  67.   /**
  68.    * Assignment
  69.    */
  70.   KRandomSequence &operator=(const KRandomSequence &a);
  71.  
  72.   /**
  73.    * Restart the sequence based on lngSeed.
  74.    * @param lngSeed Seed to initialize the sequence with.
  75.    * If lngSeed is 0, the sequence is initialized with a value from
  76.    * KApplication::random().
  77.    */
  78.   void setSeed( long lngSeed = 0 );
  79.  
  80.   /**
  81.    * Get the next number from the pseudo-random sequence.
  82.    *
  83.    * @return a pseudo-random double value between [0,1[
  84.    */
  85.   double getDouble(); 
  86.   
  87.   /**
  88.    * Get the next number from the pseudo-random sequence.
  89.    *
  90.    * @return a pseudo-random integer value between [0, max[
  91.    * with 0 <= max < 1.000.000
  92.    */
  93.   unsigned long getLong(unsigned long max); 
  94.  
  95.   /**
  96.    * Get a boolean from the pseudo-random sequence.
  97.    *
  98.    * @return a boolean which is either true or false
  99.    */
  100.   bool getBool(); 
  101.  
  102.   /**
  103.    * Put a list in random order.
  104.    *
  105.    * @param list the list whose order will be modified
  106.    */
  107.   void randomize(QGList *list);
  108.  
  109.   /**
  110.    * Modulate the random sequence. 
  111.    *
  112.    * If S(i) is the sequence of numbers that will follow 
  113.    * given the current state after calling modulate(i), 
  114.    * then S(i) != S(j) for i != j and   
  115.    *      S(i) == S(j) for i == j.
  116.    *
  117.    * This can be useful in game situation where "undo" restores
  118.    * the state of the random sequence. If the game modulates the
  119.    * random sequence with the move chosen by the player, the 
  120.    * random sequence will be identical whenever the player "redo"-s 
  121.    * his or hers original move, but different when the player 
  122.    * chooses another move.
  123.    *
  124.    * With this scenario "undo" can no longer be used to repeat a 
  125.    * certain move over and over again until the computer reacts 
  126.    * with a favorable response or to predict the response for a 
  127.    * certain move based on the response to another move.
  128.    * @param i the sequence identified
  129.    */
  130.   void modulate(int i);
  131.     
  132. private:
  133.   void Draw(); // Generate the random number
  134.   long m_lngSeed1;
  135.   long m_lngSeed2;
  136.   long m_lngShufflePos;
  137.  
  138.   static const int    m_nShuffleTableSize;
  139.   long *m_ShuffleArray;
  140.  
  141.   KRandomSequencePrivate *d;
  142. };
  143.  
  144. #endif
  145.  
  146.